package com.ym.medical.service.impl;

import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.core.utils.yunxin.YunXinApiUtil;
import com.ym.medical.dao.InquiryOrderContentDao;
import com.ym.medical.domain.entity.DoctorServicePriceEntity;
import com.ym.medical.domain.entity.DoctorStudioEntity;
import com.ym.medical.domain.entity.InquiryOrderContentEntity;
import com.ym.medical.domain.entity.InquiryOrderEntity;
import com.ym.medical.domain.entity.OrderFlowRecordEntity;
import com.ym.medical.domain.entity.PatientDetailEntity;
import com.ym.medical.domain.entity.TextRuleEntity;
import com.ym.medical.domain.entity.UserEntity;
import com.ym.medical.domain.entity.YunxinUserEntity;
import com.ym.medical.domain.param.YunxinUserParam;
import com.ym.medical.domain.vo.DoctorDetailAllInfoVo;
import com.ym.medical.domain.vo.InquiryOrderContentVo;
import com.ym.medical.service.IDoctorServicePriceSV;
import com.ym.medical.service.IDoctorStudioSV;
import com.ym.medical.service.IInquiryOrderContentSV;
import com.ym.medical.service.IOrderFlowRecordSV;
import com.ym.medical.service.IPatientDetailSV;
import com.ym.medical.service.IResultDoctorSV;
import com.ym.medical.service.ITextRuleSV;
import com.ym.medical.service.IUserSV;
import com.ym.medical.service.IYunxinUserSV;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * ComputerName:LAPTOP-6GI4HOI6
 * Author:625
 * Date: 2019-07-11
 * Time: 09:49:02
 * 问诊内容扩展表服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InquiryOrderContentSVImpl extends BaseSVImpl<InquiryOrderContentEntity, InquiryOrderContentDao> implements IInquiryOrderContentSV {

	private Logger logger = LoggerFactory.getLogger(InquiryOrderContentSVImpl.class);

	@Autowired
	private IYunxinUserSV yunxinUserSV;

	@Autowired
	private IPatientDetailSV patientDetailSV;

	@Autowired
	private IUserSV userSV;

	@Autowired
	private IResultDoctorSV resultDoctorSV;

	@Autowired
	private ITextRuleSV textRuleSV;

	@Autowired
	private IOrderFlowRecordSV orderFlowRecordSV;

	@Autowired
	private IDoctorStudioSV doctorStudioSV;

	@Autowired
	private IDoctorServicePriceSV doctorServicePriceSV;


	@Override
	public List<InquiryOrderContentVo> listContent(Long orderId) {
		return mapper.listOrderContent(orderId);
	}

	@Override
	@Async
	public void sendExpandMsg2Doctor(InquiryOrderEntity order, Long roomId, Long doctorId) {
		Long orderId = order.getId();
		Long userId = order.getUserId();
		Long patientId = order.getPatientId();
		logger.info("发送后续消息");
		List<InquiryOrderContentVo> inquiryOrderContents = mapper.listOrderContent(orderId);
		// 发送模板基础信息获取
		YunxinUserEntity doctorYunXin = yunxinUserSV.findByModel(YunxinUserParam.builder().userId(doctorId).build());
		YunxinUserEntity userYunXin = yunxinUserSV.findByModel(YunxinUserParam.builder().userId(userId).build());
		PatientDetailEntity patientDetail = patientDetailSV.findById(patientId);
		UserEntity userInfo = userSV.findById(userId);
		//获取抢单人工作室所属人的ID
		Long showSendDoctorId = doctorId;
		OrderFlowRecordEntity orderFlow = orderFlowRecordSV.findNewByOrderId(orderId);
		if (orderFlow != null && orderFlow.getDoctorDsId() != null) {
			DoctorStudioEntity doctorStudio = doctorStudioSV.selectById(orderFlow.getDoctorDsId());
			showSendDoctorId = doctorStudio.getBelongPerson();
		}
		DoctorDetailAllInfoVo doctorDetailAllInfoVo = resultDoctorSV.selectDoctorDetail(showSendDoctorId);
		DoctorServicePriceEntity doctorServicePrice = doctorServicePriceSV.selectById(order.getRuleId());
		TextRuleEntity textRule = textRuleSV.selectById(doctorServicePrice.getRuleId());
		// 1.要给医生发送的扩展消息
		if (!inquiryOrderContents.isEmpty()) {
			Map<String, Object> extMap = new HashMap<>();
			extMap.put("counsellingRoomId", roomId.toString());
			extMap.put("orderId", orderId.toString());
			extMap.put("sendPerson", userId.toString());
			extMap.put("patientHead", userInfo.getHeadUrl());
			// 表示患者后续发的扩展消息
			extMap.put("signal", "1");
			for (InquiryOrderContentVo inquiryOrderContent : inquiryOrderContents) {
				logger.info("发送扩展云信消息：{}", orderId);
				String msgType = "0";
				String content = inquiryOrderContent.getContent();
				if (StringUtils.isNotBlank(inquiryOrderContent.getAttach())) {
					content = inquiryOrderContent.getAttachInfo();
					if ("IMG".equals(inquiryOrderContent.getAttachType())) {
						msgType = "1";
					} else if ("REC".equals(inquiryOrderContent.getAttachType())) {
						msgType = "100";
					} else if ("VIDEO".equals(inquiryOrderContent.getAttachType())) {
						msgType = "3";
					}
				}
				YunXinApiUtil.sendMsg(userYunXin.getAccid(), doctorYunXin.getAccid(), msgType, content, extMap);

			}
		}
		// 2.要给患者发送的扩展消息
		Map<String, Object> extMap2 = new HashMap<>();
		extMap2.put("counsellingRoomId", String.valueOf(roomId));
		extMap2.put("orderId", String.valueOf(orderId));
		extMap2.put("sendPerson", String.valueOf(doctorId));
		extMap2.put("chatMaxNum", textRule.getTrRound());
		extMap2.put("patientHead", userInfo.getHeadUrl());
		// 表示医生给患者发的接单消息
		extMap2.put("signal", "2");
		Map<String, String> patientInfo = new HashMap<>();
		patientInfo.put("doctCategoryName", doctorDetailAllInfoVo.getDoctCategoryName());
		patientInfo.put("doctorHead", doctorDetailAllInfoVo.getHeadUrl());
		patientInfo.put("doctorName", doctorDetailAllInfoVo.getRealName());
		patientInfo.put("doctorType", doctorDetailAllInfoVo.getJobTitleName());
		patientInfo.put("hosLevel", doctorDetailAllInfoVo.getHosLevel());
		patientInfo.put("hosName", doctorDetailAllInfoVo.getHosName());
		// todo 计算年龄
		patientInfo.put("patientAge", "18");
		patientInfo.put("patientName", patientDetail.getName());
		patientInfo.put("patientSexName", patientDetail.getSex());
		extMap2.put("patientInfo", patientInfo);
		YunXinApiUtil.sendMsg(doctorYunXin.getAccid(), userYunXin.getAccid(), "0", "医生正在查看你的病情，马上回复你，请稍后...", extMap2);
	}

}
